// pages/user/userHome/userHome.js
var app = getApp();
var auth_success = app.globalData._network_path + 'auth_success.png';
var workInfo= '';//经销商信息
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userBanner: app.globalData.qi_niu_url + '/centerback.png',
    showTest: auth_success,
    usermagInfo: '', //用户信息
    getOrderNum: {}, //订单数量
    login_status: false,
    module_switch:{},
    popShow:false,
    authInfo: {'status_name':'待认证',status:''},
    identityInfo: '', // 用户身份信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    app.loginStatus(function() {
      that.setData({
        login_status: true
      })
      that.getUserInfo();
      that.getOrderStatus();
      that.getAuthInfo();
      that.getIdentityInfo(); // 获取用户身份
    }, function() {
      that.setData({
        login_status: false
      })
    })
    that.module_switch_config();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /*** 页面相关事件处理函数--监听用户下拉动作*/
  onPullDownRefresh: function() {
    var that = this;
    console.log('下拉*******');
    that.onShow();

  },
   // 获取用户身份信息
   getIdentityInfo() {
    var that = this;
    var user_id = wx.getStorageSync('user_id');
    app.ajax({
      url: 'User/Channel/getUserIdentityInfo',
      data: {
        user_id: user_id
      },
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            identityInfo: res.data.data
          })
        }
      }
    })
  },
  //跳转我的银行卡
  goCard(){
    var that = this;
    wx.navigateTo({
      url: '../presentAccount/presentAccount?workers_id=' + workInfo.workers_id,
    })
  },
  //跳转设置
  settingfun() {
    wx.navigateTo({
      url: '../changeMsg/changeMsg',
    })
  },
  //跳转我的订单
  myOrderList() {
    wx.navigateTo({
      url: '../myOrderList/myOrderList?tables=0',
    })
  },
  //跳转我的地址
  myAddressList() {
    wx.navigateTo({
      url: '../myAddressList/myAddressList?on_way=' + 1,
    })
  },
  //跳转我的合同
  settingfun() {
    wx.navigateTo({
      url: '../changeMsg/changeMsg',
    })
  },
  //跳转资产管理
  myPinroll() {
    wx.navigateTo({
      url: '../econtractList/econtractList',
    })
  },
  //跳转积分
  myIntegral() {
    wx.navigateTo({
      url: '../myIntegral/myIntegral',
    })
  },

  //邀请好友
  invitation() {
    wx.navigateTo({
      // url: '../invitaList/invitaList',
      url: '../inviteFriends/inviteFriends',
    })
  },
  // 跳转意见反馈
  sunSingle: function() {
    wx.navigateTo({
      url: '../feedBack/feedBack',
    })

  },
  //跳转帮助中心
  myTicket: function() {
    wx.navigateTo({
      url: '../helpCenter/helpCenter'
    })
  },
  // 跳转关于我们
  myCode: function() {
    wx.navigateTo({
      url: '../aboutUs/aboutUs'
    })
  },
  // 跳转售后服务
  generalize: function() {
    wx.navigateTo({
      url: '../afterSale/afterSale?type=1'
    })
  },
  // 跳转优惠券
  myCoupon: function() {
    wx.navigateTo({
      url: '../coupon/coupon',
    })
  },
  //登录
  login: function() {
    wx.navigateTo({
      url: '../login/login',
    })
  },

  // 共享管理
  shareequipment: function() {
    wx.navigateTo({
      url: '../../sharebox/deviceList/deviceList',
    })
  },
  // 获取经销商信息
  workerInfo() {
    var that = this;
    app.ajax({
      url: "User/Spread/workerInfo",
      data: {},
      success: function (res) {
        if (res.data.code == 1000) {
          workInfo= res.data.data;
        }
      }
    })
  },
  //获取用户头像信息
  bindgetuserinfofun: function(e) {
    var that = this;
    if (e.detail.errMsg == 'getUserInfo:fail:cancel to confirm login' ||
      e.detail.errMsg == 'getUserInfo:fail auth deny' || e.detail.rawData == 'undefined') {
      // app.showToast("取消将无法修改用户信息", "none", 2000, function() {});
    } else {
      wx.setStorageSync('user_Info', e.detail.userInfo); //缓存user信息
      that.editUserInfo(e.detail.userInfo);
    }
  },

  //存储用户信息
  editUserInfo(userMsg) {
    var that = this;
    console.log('userMsg>>>', userMsg)
    app.ajax({
      url: "User/User/editUserInfo",
      data: {
        username: userMsg.nickName,
        avatar_img: userMsg.avatarUrl,
        gender: userMsg.gender,
        country: userMsg.country,
        province: userMsg.province,
        city: userMsg.city
      },
      success: function(res) {
        console.log('获取用户信息的结果', res)
        if (res.data.code == 1000) {
          // 更新用户地址
          that.updateAddress();
        } else {

        }
        that.getUserInfo(); //重新获取用户信息
      }
    })
  },

  //获取用户信息
  getUserInfo() {
    var that = this;
    app.ajax({
      url: "User/User/getUserInfo",
      data: {},
      success: function(res) {
        wx.stopPullDownRefresh()
        if (res.data.code == 1000) {
          that.setData({
            usermagInfo: res.data.data
          })
          if (res.data.data.is_dealer == 1){
            that.workerInfo();
          }
        } else {
          app.showToast(res.data.message, "none", 2000, function() {});
        }
        that.getUserth();
      }
    })
  },

  // 更新用户地址
  updateAddress: function () {
    var that = this;
    var country = wx.getStorageSync('country');
    var province = wx.getStorageSync('province');
    var city = wx.getStorageSync('city');
    var area = wx.getStorageSync('area');
    app.ajax({
      url: 'User/User/editAddress',
      data: {
        country: country,
        province: province,
        city: city,
        area: area,
        address_is_update: 2
      },
      success: function (res) {
        //console.log('更新用户地址结果', res)
      }
    })
  },

  getOrderStatus() {
    var that = this;
    app.ajax({
      url: "User/Order/getOrderStatus",
      data: {},
      success: function(res) {
        if (res.data.code == 1000) {
          that.setData({
            getOrderNum: res.data.data
          })
        } else {
          //app.showToast(res.data.message, "none", 2000, function() {});
        }
      }
    })
  },

  // 我的推广
  myPromotion(){
    wx.navigateTo({
      // url: '../myPromotion/myPromotion',
      url: '/pages/subpackage/pages/extend/extendHome/extendHome'
    })
  },

  //跳转我的认证
  authentication() {
    var that = this;
    if (that.data.authInfo.status == -2) {
      wx.navigateTo({ //../../shopping/uploadDocuments/uploadDocuments
        url: '../../shopping/uploadDocuments/uploadDocuments',
      })
    } else if (that.data.authInfo.status == -3) {
      wx.navigateTo({
        url: '../../shopping/faceRecognition/faceRecognition',
      })
    } else if (that.data.authInfo.status == 2 || that.data.authInfo.status == 1){
      wx.navigateTo({
        url: '../../myCertification/myCertification',
      })
    } else {
      wx.navigateTo({
        url: '../../shopping/realName/realName',
      })
    }

  },

  // 我的邀请码
  qrcode() {
    wx.navigateTo({
      url: '../../invitationCode/invitationCode',
    })
  },

  //获取模块状态配置
  module_switch_config(){
      var that = this;
      app.ajax({
        url: "Common/Test/moduleSwitch",
        data: {},
        success: function (res) {
          if (res.data.code == 1000) {
            that.setData({
              module_switch: res.data.data
            })
          }else{
            // app.showToast(res.data.message);
          } 
        }
      })
  },
  //获取是否有
  getUserth(){
    var that = this;
    app.ajax({
      url: "User/Realname/getAuthStatus",
      data: {},
      success: function (res) {
        if (res.data.code == 1000) {
          var look_auth=wx.getStorageSync('lookAuth');
          if (res.data.data.is_auth || res.data.data.order_count > 0 || res.data.data.work_count > 0 || look_auth){
            that.setData({
              popShow: false
            })
          }else{
            that.setData({
              popShow: true
            })
          }
         
        } else {
          
        }
      }
    })
  },
  //关闭弹窗
  closeFun(){
    var that=this;
    wx.setStorageSync('lookAuth', true);
    that.setData({
      popShow:false
    })
  },
    //申请成为渠道
    channel_apply(){
      // 获取渠道申请详情
      var that = this;
      var user_id = wx.getStorageSync('user_id');
      app.ajax({
        url: 'User/Channel/getApplyInfo',
        data: {
          user_id: user_id
        },
        success: function (res) {
          if (res.data.code == -1000) {
            wx.navigateTo({
              url: '../../channel/apply/apply',
            })
          } else if (res.data.code == 1000) {
            wx.navigateTo({
              url: '../../channel/applyResult/applyResult',
            })
          }
        }
      })
    },
    //我的渠道
    my_channel() {
      wx.navigateTo({
        url: '../../channel/home/home',
      })
    },
  //获取用户认证信息
  getAuthInfo() {
    var that = this;
    app.ajax({
      url: "User/Realname/getAuthInfo",
      data: {},
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            authInfo: res.data.data
          }) 
        }else{
          that.setData({
            authInfo: {'status_name':'待认证','status':''}
          })
        }
      }
    })
  }

})